package ClassicalCryptography;

import java.math.BigInteger;

public class VernamCipher {
    public String encrypt(String plaintext, String key) {
        BigInteger pt = new BigInteger(plaintext, 16);
        BigInteger k = new BigInteger(key, 16);
        StringBuilder res = new StringBuilder(pt.xor(k).toString(16));
        while (res.length() < plaintext.length()) {
            res.insert(0, '0');
        }
        return res.substring(0);
    }

    public String decrypt(String ciphertext, String key) {

        BigInteger pt = new BigInteger(ciphertext, 16);
        BigInteger k = new BigInteger(key, 16);
        StringBuilder res = new StringBuilder(pt.xor(k).toString(16));
        while (res.length() < ciphertext.length()) {
            res.insert(0, '0');
        }
        return res.substring(0);
    }
}
